package ysq.xyg.sale.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sale.dao.SaleProductMyDao;
import ysq.xyg.sale.model.po.SaleProductMy;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sale.model.vo.SaleProductMyVo;
import ysq.xyg.sale.service.SaleProductMyService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 销售模块-我的产品service实现
 *
 * @author shengQiang yu
 * @date 2019-10-9 14:57:59
 */
@Service("saleProductMyService")
public class SaleProductMyServiceImpl extends BaseServiceImpl<SaleProductMy> implements SaleProductMyService {

    @Resource
    private SaleProductMyDao saleProductMyDao;

    private SaleProductMy saleProductMy;

    @Autowired
    public void setBaseDao(SaleProductMyDao saleProductMyDao) {
        this.baseDao = saleProductMyDao;
    }


    /**
     * 分页查询
     *
     * @param saleProductMyVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-9 14:57:59
     */
    public ReturnEntity page(SaleProductMyVo saleProductMyVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SaleProductMy> list = acceptList(saleProductMyVo);
        PageInfo<SaleProductMy> page=new PageInfo<SaleProductMy>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param saleProductMyVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-9 14:57:59
     */
    public ReturnEntity addOrUpdate(SaleProductMyVo saleProductMyVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(saleProductMyVo.getId())){
            saleProductMy=new SaleProductMy(currentUser);
            isUpate=false;
        }else{
            saleProductMy=selectByPrimaryKey(saleProductMyVo.getId());
            if(ObjectUtil.isNull(saleProductMy)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            saleProductMy.setUpdateTime(new Date());
            saleProductMy.setUpdateUser(currentUser.getId());
        }
        saleProductMy.setName(saleProductMyVo.getName());
        saleProductMy.setThumImage(saleProductMyVo.getThumImage());
        saleProductMy.setHomeUrl(saleProductMyVo.getHomeUrl());
        saleProductMy.setUserUrl(saleProductMyVo.getUserUrl());
        saleProductMy.setUseEndTime(saleProductMyVo.getUseEndTime());
        saleProductMy.setProductId(saleProductMyVo.getProductId());
        saleProductMy.setUserId(saleProductMyVo.getUserId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(saleProductMy);
        }else{
            num=insert(saleProductMy);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 修改
     *
     * @param saleProductMyVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-9 14:57:59
     */
    public ReturnEntity updateByName(SaleProductMyVo saleProductMyVo, SysUser currentUser) {
        saleProductMy=selectByPrimaryKey(saleProductMyVo.getId());
        if(ObjectUtil.isNull(saleProductMy)){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        saleProductMy.setUpdateTime(new Date());
        saleProductMy.setUpdateUser(currentUser.getId());
        saleProductMy.setName(saleProductMyVo.getName());
        saleProductMy.setThumImage(saleProductMyVo.getThumImage());
        int  num=updateByPrimaryKey(saleProductMy);
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-9 14:57:59
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SaleProductMy saleProductMy=selectByPrimaryKey(id);
        if(saleProductMy!=null){
            saleProductMy.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            saleProductMy.setUpdateTime(new Date());
            saleProductMy.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(saleProductMy);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param saleProductMyVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-10-9 14:57:59
     */
    public List<SaleProductMy> acceptList(SaleProductMyVo saleProductMyVo){
        Example example = new Example(SaleProductMy.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(saleProductMyVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",saleProductMyVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(saleProductMyVo.getName())){
            criteria.andLike("name","%"+saleProductMyVo.getName()+"%");
        }
        if(StringUtil.isNotEmpty(saleProductMyVo.getUserId())){
            criteria.andEqualTo("userId",saleProductMyVo.getUserId());
        }
        example.setOrderByClause(" update_time desc");
        return saleProductMyDao.selectByExample(example);
    }
}
